//
//  HICEmoticonPageViewCell.swift
//  vibo
//
//  Created by jht-seven on 16/7/5.
//  Copyright © 2016年 jht-seven. All rights reserved.
//

import UIKit

class HICEmoticonPageViewCell: UICollectionViewCell {
    
    var emoticons:[HICEmoticon]?{
        didSet{
            
            
            // 当每次外界给其赋值的时候 要吧当前cell中的20个按钮全部隐藏（由于cell复用问题）
            // 遍历按钮隐藏
            for button in emoticonButtons {
                button.hidden = true
            }
            
            // 遍历外界传入的一维数组
            for (i,emoticon) in emoticons!.enumerate() {
                // 获取button
                let button = emoticonButtons[i]
                // 显示button
                button.hidden = false
                
                /*
                 - 如果type ==  0 代表是图片
                 - 如果type == 1 代表是字符串
                 */
                //代表是emoji表情
                if emoticon.isEmoji {
                    let tit = ((emoticon.code ?? "") as NSString).emoji()
                    
                    button.setTitle(tit, forState: UIControlState.Normal)
                    // 考虑到cell重用问题
                    button.setImage(nil, forState: UIControlState.Normal)
                }else {
                    
                    let path = emoticon.path ?? ""
                    let png = emoticon.png ?? ""
                    let name = path + png
                    print(name)
                    let image = UIImage(named: name, inBundle: HICEmoticonTools.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
                    // 代表图片表情
                    button.setImage(image, forState: UIControlState.Normal)
                    // 考虑到cell重用问题
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
                
            }
        }
    }
    
    
    // 保存20个表情按钮
    var emoticonButtons:[UIButton] = [UIButton]()
    
    var indexPath: NSIndexPath?{
        didSet{
            messageLabel.text = "第\(indexPath?.section ?? 0)组 第\(indexPath?.item ?? 0)页"
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RandomColor()
        addChildButtons()
        //
        contentView.addSubview(messageLabel)
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(contentView)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 记得按钮的宽度高度
        let w = SCREENW / CGFloat(EmoticonMaxCol)
        let h = (216 - 37) / CGFloat(EmoticonMaxRow)
        
        // 遍历emoticonButtons
        for (i , button) in emoticonButtons.enumerate() {
            let x = i % EmoticonMaxCol
            let y = i / EmoticonMaxCol
            // 设置buttonframe
            button.frame = CGRect(x: CGFloat(x) * w , y: CGFloat(y) * h, width: w, height: h)
        }
        
        
    }
    
    
    // 创建20个按钮
    func addChildButtons(){
        for _ in 0..<EmoticonMaxCount {
            // 创建button
            let button = UIButton()
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            // 设置随机颜色
            button.backgroundColor = RandomColor()
            // 添加到数组中
            emoticonButtons.append(button)
            // 添加button
            contentView.addSubview(button)
        }
    }
    
    // MARK: - 懒加载控件
    private lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.redColor(), textFont: 35)
        lab.textAlignment = .Center
        return lab
    }()
}
